package com.wing.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseOrder;
import com.wing.product.model.request.WarehouseOrderForm;
import com.wing.product.model.response.WarehouseOrderVO;
import com.wing.product.model.response.WarehouseOutboundOrderVO;
import com.wing.product.service.WarehouseOrderService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* (warehouse_product)表控制层
*
* @author xxxxx
*/
@Api(value = "托管入库管理", tags = "#/warehouse/product/order")
@RestController("WarehouseOrderController")
@RequestMapping("/warehouse/order")
@RequiredArgsConstructor
public class WarehouseOrderController {

    private final WarehouseOrderService warehouseOrderService ;


    @ApiOperation(value = "查看托管订单", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseOrderVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order
            ,String warehouseCustomerId,
            String customerName,
            String customerCode,
            String createTime,
            String filterRemainder
    ){

        Page<WarehouseOrder> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        //判断客户端，如果是会员端，则需要添加对应的部门信息作为查询条件
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
            paramMap.put("warehouseCustomerId",JwtUtils.getDepartmentId());
        }else{
            if(!StringUtils.isEmpty(warehouseCustomerId)){
                paramMap.put("warehouseCustomerId",warehouseCustomerId);
            }
        }
        //TODO
        if(!StringUtils.isEmpty(customerName)){
            paramMap.put("customerName",customerName);
        }
        if(!StringUtils.isEmpty(customerCode)){
            paramMap.put("customerCode",customerCode);
        }
        if(!StringUtils.isEmpty(createTime)){
            paramMap.put("createTime",createTime);
        }
        if(!StringUtils.isEmpty(filterRemainder)){
            paramMap.put("filterRemainder",filterRemainder);
        }
        //搜索
        IPage<WarehouseOrderVO> list = warehouseOrderService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "根据客户查看托管订单", notes = "搜索")
    @GetMapping("/findListByCustomerId")
    public Page<WarehouseOrderVO> findListByCustomerId(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order
            ,String warehouseCustomerId
    ){

        Page<WarehouseOrder> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("warehouseCustomerId",warehouseCustomerId);
        //搜索
        return warehouseOrderService.pageList4app(pg,paramMap);
    }


    @ApiOperation(value = "添加托管订单", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(WarehouseOrderForm.Add.class)  @RequestBody WarehouseOrderForm warehouseOrderForm)  {
        try{
            if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
                warehouseOrderForm.setCustomerId(JwtUtils.getDepartmentId());
            }
           Boolean b =  warehouseOrderService.save(warehouseOrderForm);
           if(b){
               return JsonResult.success("添加成功");
           } else {
               return JsonResult.fail("添加失败");
           }
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "更改托管订单", notes = "更改")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PutMapping("/update")
    public JsonResult update(@Validated(WarehouseOrderForm.Add.class)  @RequestBody WarehouseOrderForm warehouseOrderForm)  {
        try{
            if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
                warehouseOrderForm.setCustomerId(JwtUtils.getDepartmentId());
            }
            Boolean b =  warehouseOrderService.update(warehouseOrderForm);
            if(b){
                return JsonResult.success("添加成功");
            } else {
                return JsonResult.fail("添加失败");
            }
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "受理", notes = "启用")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/updateStatus")
    public JsonResult updateStatus(Long id , Integer status) {
        WarehouseOrder attr=new WarehouseOrder();
        attr.setId(id);
        // 设置受理成功
        attr.setStatus(status);
        if (warehouseOrderService.updateById(attr)) {
//            return JsonResult.success("受理成功");
            return JsonResult.success("操作成功");
        }
        return JsonResult.fail("操作失败");
    }

    @ApiOperation(value = "受理订单", notes = "受理订单")
    @RequestObjectFieldIgnore({"id", "chargingTime"})
    @GetMapping("/acceptance")
    @Transactional
    public JsonResult acceptance(Long id ,  String chargingTime) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = dateFormat.parse(chargingTime);
        WarehouseOrder attr=new WarehouseOrder();
        attr.setId(id);
        // 设置受理成功
        attr.setStatus(2);
        attr.setChargingTime(date);
        if (warehouseOrderService.updateById(attr)) {
//            return JsonResult.success("受理成功");
            return JsonResult.success("操作成功");
        }
        return JsonResult.fail("操作失败");
    }

    @ApiOperation(value = "详情", notes = "详情")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/info")
    public JsonResult info(Long id ) {
        return JsonResult.success(warehouseOrderService.findOrderInfoById(id ,"order"));
    }

}
